var chooseGoods={
    current:1,
    init:function(){
        var _t=this;
        _t.getTotalPrice();
        $("#J_btnGoStep1").on("click",function(){
            var otherPJ="";
            $(".zhoubian").find(".current").each(function(){
                otherPJ+=otherPJ==""?$(this).attr("data-id"):","+$(this).attr("data-id");
            })

            var main=$("#J_level"+_t.current).find("li");
            $("#J_jyId").val(main.eq(0).attr("data-id"));
            if($("#J_jvId").val()=="278"&&$("#J_valueJY").html()!=""){
                alert("抱歉，机滤暂时缺货！");
                return false;
            }

            $("#J_otherId").val(otherPJ);
            $("#J_myForm").submit();
        })

    },

    toServer:function(index){
        index--;
        if(index==0&&(autoId==null||autoId=="")){
            window.location.href="/mobile/step1?choose=true&back=home";
            return false;
        }

        
        $("menu").find("li").eq(index).addClass("current").siblings("li").removeClass("current");
        this.current=index;
        $(".serverMain").css("marginLeft","-"+(100*index)+"%");
        this.getTotalPrice();
    },
    showList:function(str){
        window.scrollTop=$("body").scrollTop();
        $("body").addClass("overflow-H");
        $(str).show();
    },
    hideList:function(str){
        $("body").removeClass("overflow-H").scrollTop(window.scrollTop);
        $(str).hide();
    },

    //选择机油
    chooseJY:function(id,t){
        var _t=this;
        $(t).addClass("current").siblings("li").removeClass("current");
        $("#J_goodsListJY").hide();
        $.ajax({
            type:"post",
            url:"/baoyang/chooseJY.html",
            data:{"pj_id":id},
            dataType:"json",
            success:function(res){
                if(res.status==1){
                    _t.printJY(res.data);
                }else{
                    showMsgTip(res.info,2000);
                    return false;
                }
            }

        })
    },

    //输出选择后的机油数据
    printJY:function(data){
        if(data['four']['pj_id']==data['one']['pj_id']){
            //返回数据都为1升数据时（推荐全部以1升装出售）
            var htmlFour='<a href="javascript:void(0)" class="iconfont more">&#xe603;</a>'+
                    '<img src="http://app2.haixiuyizhan.com/'+data['four']['logo']+'">'+
                    '<p class="name">'+data['four']['pj_name']+" "+data['four']['weight']+'L*4</p>'+
                    '<p class="price"><em class="J_price">￥'+(data['four']['pj_csprice']*4)+'</em><span>￥'+(data['four']['pj_scprice']*4)+'</span></p>';
        }else{
            var htmlFour='<a href="javascript:void(0)" class="iconfont more">&#xe603;</a>'+
                    '<img src="http://app2.haixiuyizhan.com/'+data['four']['logo']+'">'+
                    '<p class="name">'+data['four']['pj_name']+" "+data['four']['weight']+'L*1</p>'+
                    '<p class="price"><em class="J_price">￥'+data['four']['pj_csprice']+'</em><span>￥'+data['four']['pj_scprice']+'</span></p>';
        }
        

        var menu=$("menu").find("li"),
            index=$("menu").find(".current").index();

        $("#J_jyId").val(data['four']['pj_id']);

        //根据所选机油，跳至相应套餐并替换机油
        if(index==0){
            index==0
        }else if(data['four']['pj_csprice']>300||data['one']['pj_id']==287){
            index=3;
        }else if(data['four']['pj_csprice']>200){
            index=1;
        }else if(data['four']['pj_csprice']>100){
            index=2;
        }

        this.current=index;
        menu.eq(index).attr("data-jyId",data['four']['pj_id']).click();
        $("#J_level"+index).find("li").eq(0).html(htmlFour).attr("data-id",data['four']['pj_id']);


        //输出附加项
        valueJY=$("#J_valueJY").html();
        if(valueJY!=""&&valueJY!=4){
            htmlOther="<p class='other'>"+$("p.other").html()+"</p>";   //额外机油量
            var f=Math.floor((valueJY-4)/4),
                o=(valueJY-4)%4;
            if(f>0&&data['four']['pj_id']==data['one']['pj_id']){
                htmlOther+='<div class="otherMain" data-id="'+data['four']['pj_id']+'">'+
                                '<img src="http://app2.haixiuyizhan.com/'+data['four']['logo']+'" alt="">'+
                                '<p class="name">'+data['four']['pj_name']+" "+data['four']['weight']+'L*'+(f*4)+'</p>'+
                                '<p class="price"><em class="J_price">￥'+data['four']['pj_csprice']*f*4+'</em><span>￥'+data['four']['pj_scprice']*f*4+'</span></p>'+
                            '</div>';
            }else if(f>0){
                htmlOther+='<div class="otherMain" data-id="'+data['four']['pj_id']+'">'+
                                '<img src="http://app2.haixiuyizhan.com/'+data['four']['logo']+'" alt="">'+
                                '<p class="name">'+data['four']['pj_name']+" "+data['four']['weight']+'L*'+f+'</p>'+
                                '<p class="price"><em class="J_price">￥'+data['four']['pj_csprice']*f+'</em><span>￥'+data['four']['pj_scprice']*f+'</span></p>'+
                            '</div>';
            }

            if(o!=0){
                htmlOther+='<div class="otherMain" data-id="'+data['one']['pj_id']+'">'+
                                '<img src="http://app2.haixiuyizhan.com/'+data['one']['logo']+'" alt="">'+
                                '<p class="name">'+data['one']['pj_name']+" "+data['one']['weight']+'L*'+o+'</p>'+
                                '<p class="price"><em class="J_price">￥'+data['one']['pj_csprice']*o+'</em><span>￥'+data['one']['pj_scprice']*o+'</span></p>'+
                            '</div>';
            }
            $("#J_level"+index).find("div.other").html(htmlOther);
        }
        

        this.getTotalPrice();
    },

    //选择机滤
    chooseJV:function(id,t){
        t=$(t);
        t.addClass("current").siblings("li").removeClass("current");
        $("#J_goodsListJV").hide();

        var menu=$("menu").find(".current"),
            index=menu.index(),
            dom=$("#J_level"+index).find("li:eq(1)"),
            logo=t.find("img").attr("src"),
            name=t.find(".name").html(),
            price=t.find(".price > em").html(),
            priceM=t.find(".price > span").html();

        dom.find(".name").html(name);
        dom.find("img").attr("src",logo);
        dom.find(".price > em").html(price);
        dom.find(".price > span").html(priceM);
        menu.attr("data-jvId",id);
    },

    chooseGoods:function(t){
        t=$(t);
        if(t.hasClass("current")){
            t.removeClass("current");
        }else{
            t.addClass("current");
        }
        this.getTotalPrice();
        
    },
    chooseCoupon:function(t){
        t=$(t);
        if(t.hasClass("current")){
            t.removeClass("current");
        }else{
            t.addClass("current").siblings("li").removeClass("current");
        }
        this.getTotalPrice();
    },
    getTotalPrice:function(){
        var total=typeof $("#J_total").val()=="undefined"?0:$("#J_total").val(),
            totalJY=0,
            price=0,
            html="";
        //首页时计算当前选择项
        if(total==0){
            $("#J_level"+this.current).find(".J_price").each(function(i){
                price=$(this).html().replace(/\D/g,"")*1
                if(i!=1){
                    totalJY+=price;
                }
                total+=price;
            })

            $(".zhoubian").find(".current").find(".J_price").each(function(){
                total+=$(this).html().replace(/\D/g,"")*1;
            })
            $("#J_priceJY").val(totalJY);

            //勾选只买配件
            var fourModel=$("#fourModel");
            if(!$(".server h4").find(".select").hasClass("selected")&&typeof fourModel[0]!="undefined"){
               // total+=fourModel.find(".price").html().replace(/\D/g,"")*1;
                $("#J_wx_id").val("441");
            }
        }

        $(".totalPrice").html("总价：￥"+total);

        //有选择的优惠券
        var coupon=$(".couponList").find(".current"),
            html="";

        if(typeof coupon[0]!="undefined"){

            var couponType=coupon.attr("data-type")*1,
                couponVal=coupon.attr("data-val")*1,
                couponLow=coupon.attr("data-low")*1;
            if(couponType==1){  //满抵
                total=total*1;
                if(total>=couponLow){
                    totalPrice=total-couponVal;
                    $(".couponPrice").html("优惠：￥"+couponVal);
                }
            }else if(couponType==4&&this.couponStatus==1){    //免单券逻辑
                $(".couponPrice").html("优惠：￥"+(couponVal));
                totalPrice=total-couponVal;
                //html="免"+youhui+"元";
                //html+=couponVal-youhui>0?"，<span>还可免"+(couponVal-youhui)+"元</span>":"";
            }

        }else{
            $(".couponPrice").html("优惠：￥0");
            
            totalPrice=total;
        }


        $("#J_totalPrice").val(totalPrice);

        if(this.couponStatus=="2"&& typeof $("#J_totalPrice")[0]=="undefined"){
            html='<span>*</span>免单券可在下次消费时使用';
            $(".couponTxt").html(html);
            
        }else{
            var mPrice=0,
                txt="";
            $("#J_level"+this.current).find(".price").find("span").each(function(i){
                mPrice+=$(this).html().replace(/\D/g,"")*1;
            })

            $(".zhoubian").find(".current").find(".price").find("span").each(function(){
                mPrice+=$(this).html().replace(/\D/g,"")*1;
            })

            txt=this.couponStatus==1?"最高可免￥"+this.couponMoney:(this.couponStatus==2?"买多少返多少":"");

            html="<p class='price'><span class='mPrice'>￥"+mPrice+"</span><em>￥"+totalPrice+"</em></p><p class='txt'>"+txt+"</p>";
            $(".index").html(html);
        }

        
        $("#J_payPrice").html("￥"+totalPrice);
        $("#J_getPrice").html(totalPrice>1000?1000:totalPrice+"元");
        
    }
 }
